package com.conn3cted.phrasebook.web.mobile;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.conn3cted.phrasebook.domain.Person;
import com.conn3cted.phrasebook.web.BaseController;

@RequestMapping("/m")
@Controller
// @SessionAttributes("person")
public class MLoginController extends BaseController {
		
	@RequestMapping(method = RequestMethod.GET)
	public String returnHome(){		
		return "mobile/login";
	}

	@RequestMapping(value = "login" , method = RequestMethod.POST)
	public String onSubmit(@RequestParam("username") String username,
			@RequestParam("password") String password, Model uiModel) {

		if (Person.findPerson(username, password) != null) {

			contextModel.setPerson(Person.findPerson(username, password));
			
			List<Person> people = Person.findAllPeople();
			uiModel.addAttribute("people", people);
			
			return "mobile/list";
		}

		uiModel.addAttribute("error", "error_login");
		return "mobile/login";

	}

	@RequestMapping("/signOut")
	public String signOut(Model uiModel) {
		contextModel.setPerson(null);
		return "mobile/login";
	}
}
